package com.stars.controller.company;

import com.github.pagehelper.PageInfo;
import com.stars.common.annotation.Log;
import com.stars.common.ObjectCopyUtils;
import com.stars.common.base.BaseConstant;
import com.stars.common.base.BaseMsg;
import com.stars.common.base.ResultMsg;
import com.stars.common.enums.OperationType;
import com.stars.common.valid.AddView;
import com.stars.common.valid.UpdateView;
import com.stars.controller.BaseController;
import com.stars.entity.company.CompanyEntity;
import com.stars.entity.dto.company.CompanyDTO;
import com.stars.services.company.CompanyAccessServices;
import com.stars.services.company.CompanyConfigServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Title:  mybatis dao
 * @author dj
 * @date 2019-09-30 13:00:05
 * @version V1.0
 *
 */
@Slf4j
@RestController
@RequestMapping("/companyCtl")
@Api(tags ="")
public class CompanyCtl extends BaseController{

   @Autowired
   private CompanyAccessServices  companyAccessServicesImpl;
   @Autowired
   private CompanyConfigServices  companyConfigServicesImpl;
   /**
   *  分页查询
   * @param pageNum 起始页
   * @param pageSize 大小
   * @return 分页数据
   * @throws Exception
   */
   @GetMapping("/pageList")
   @ApiOperation(value = ""+BaseMsg.MODULAR_PAGE,notes =""+ BaseMsg.MODULAR_PAGE)
   @Log(detail = ""+ BaseMsg.MODULAR_PAGE,operationType = OperationType.SELECT)
   public PageInfo<CompanyDTO> pageCompanyList(Integer pageNum, Integer pageSize, CompanyDTO dto,HttpServletRequest request) throws Exception{

        return companyAccessServicesImpl.pageList(pageNum,pageSize,dto);

   }
    /**
    * 保存
    * @param entityName
    * @return 成功失败
    * @throws Exception
    */
    @PostMapping(value = "/add")
    @ApiOperation(value = ""+ BaseMsg.MODULAR_ADD,notes = ""+BaseMsg.MODULAR_ADD)
    @Log(detail = ""+BaseMsg.MODULAR_ADD,operationType = OperationType.INSERT)
    public ResultMsg addCompany(@Validated({AddView.class}) CompanyEntity entityName,HttpServletRequest request) throws Exception{
        CompanyEntity company = new CompanyEntity();
        ObjectCopyUtils.copyPropertiesIf(entityName,company, BaseConstant.EXCLUDES_BASE);
        //setAddBase(entityName,request);
        if(StringUtils.isEmpty(entityName.getPid())){
            entityName.setPid("root");
        }
        return companyConfigServicesImpl.insert(entityName);
    }
    /**
    * 更新
    * @param entityName
    * @return 成功失败
    * @throws Exception
    */
    @PostMapping(value = "/update")
    @ApiOperation(value = ""+ BaseMsg.MODULAR_UPDATE,notes = ""+ BaseMsg.MODULAR_ADD)
    @Log(detail = ""+ BaseMsg.MODULAR_UPDATE,operationType = OperationType.UPDATE)
    public ResultMsg updateCompany(@Validated({UpdateView.class}) CompanyEntity entityName,HttpServletRequest request) throws Exception{
        CompanyEntity entity = companyAccessServicesImpl.selectById(entityName.getId());
        ObjectCopyUtils.copyPropertiesIf(entityName,entity, BaseConstant.EXCLUDES_BASE);
        //setUpdateBase(entity,request);
        return companyConfigServicesImpl.updateSelectiveById(entity);

    }
    /**
    * 根据id查询
    * @param id
    * @return 查询对象
    * @throws Exception
    */
    @GetMapping(value = "/findById")
    @ApiOperation(value = ""+ BaseMsg.MODULAR_FIND_ID,notes = ""+BaseMsg.MODULAR_FIND_ID)
    @Log(detail = ""+ BaseMsg.MODULAR_FIND_ID,operationType = OperationType.SELECT)
    public ResultMsg findCompanyById(String id) throws Exception{
        CompanyEntity entityName = companyAccessServicesImpl.selectById(id);
         return ResultMsg.success(entityName);
    }
     /**
     * 删除
     * @param ids
     * @return 成功失败
     * @throws Exception
     */
     @PostMapping(value = "/delById")
     @ApiOperation(value = ""+BaseMsg.MODULAR_DEL,notes =""+BaseMsg.MODULAR_FIND_ID)
     @Log(detail =""+BaseMsg.MODULAR_DEL,operationType = OperationType.DELETE)
     public ResultMsg delCompanyById(String ids) throws Exception{
         try {
            return companyConfigServicesImpl.deleteByIds(ids.split(","));
         }catch (Exception e){
             return ResultMsg.failure(e.getMessage());
         }
     }
     /**
     * 查询树
     * @param companyDTO
     * @return 成功失败
     * @throws Exception
     */
     @PostMapping(value = "/getTree")
     @ApiOperation(value = ""+BaseMsg.MODULAR_SEARCH,notes ="参数:id,name（模糊查询）")
     @Log(detail ="查询公司树",operationType = OperationType.SELECT)
     public ResultMsg getTree(CompanyDTO companyDTO,HttpServletRequest request) throws Exception{
         return ResultMsg.success(companyAccessServicesImpl.findTree(companyDTO));
     }
}